home *** CD-ROM | disk | FTP | other *** search
- function PROT_XMLHttpRequest() {
- var Cc = Components.classes;
- var Ci = Components.interfaces;
- var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
- .createInstance(Ci.nsIXMLHttpRequest);
- request.QueryInterface(Ci.nsIJSXMLHttpRequest);
- return request;
- }
- function PROT_XMLFetcher(opt_stripCookies) {
- this.debugZone = "xmlfetcher";
- this._request = new PROT_XMLHttpRequest();
- this._stripCookies = !!opt_stripCookies;
- }
- PROT_XMLFetcher.prototype = {
- _callback: null,
- get: function(page, callback) {
- this._request.abort(); // abort() is asynchronous, so
- this._request = new PROT_XMLHttpRequest();
- this._callback = callback;
- var asynchronous = true;
- this._request.open("GET", page, asynchronous);
- if (this._stripCookies)
- new PROT_CookieStripper(this._request.channel);
- var self = this;
- this._request.onreadystatechange = function() {
- self.readyStateChange(self);
- }
- this._request.send(null);
- },
- readyStateChange: function(fetcher) {
- if (fetcher._request.readyState != 4) // TODO: check status code 200
- return;
- var responseText = null;
- try {
- G_Debug(this, "xml fetch status code: \"" +
- fetcher._request.status + "\"");
- var responseText = fetcher._request.responseText;
- } catch(e) {
- G_Debug(this, "Caught exception trying to read xmlhttprequest " +
- "status/response.");
- G_Debug(this, e);
- }
- if (fetcher._callback)
- fetcher._callback(responseText);
- }
- };
- function PROT_CookieStripper(channel) {
- this.debugZone = "cookiestripper";
- this.topic_ = "http-on-modify-request";
- this.channel_ = channel;
- var Cc = Components.classes;
- var Ci = Components.interfaces;
- this.observerService_ = Cc["@mozilla.org/observer-service;1"]
- .getService(Ci.nsIObserverService);
- this.observerService_.addObserver(this, this.topic_, false);
- var twentySeconds = 20 * 1000;
- this.alarm_ = new G_Alarm(BindToObject(this.stopObserving, this),
- twentySeconds);
- }
- PROT_CookieStripper.prototype.observe = function(subject, topic, data) {
- if (topic != this.topic_ || subject != this.channel_)
- return;
- G_Debug(this, "Stripping cookies for channel.");
- this.channel_.QueryInterface(Components.interfaces.nsIHttpChannel);
- this.channel_.setRequestHeader("Cookie", "", false /* replace, not add */);
- this.alarm_.cancel();
- this.stopObserving();
- }
- PROT_CookieStripper.prototype.stopObserving = function() {
- G_Debug(this, "Removing observer");
- this.observerService_.removeObserver(this, this.topic_);
- this.channel_ = this.alarm_ = this.observerService_ = null;
- }
- PROT_CookieStripper.prototype.QueryInterface = function(iid) {
- var Ci = Components.interfaces;
- if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserve))
- return this;
- throw Components.results.NS_ERROR_NO_INTERFACE;
- }
-